<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xami
 * Date: 12-9-19
 * Time: 下午10:07
 * To change this template use File | Settings | File Templates.
 */
$cate=Yii::app()->request->getParam('cate', 0);
$iid=Yii::app()->request->getParam('iid', 0);


$uid=Yii::app()->user->id;
$session=new CHttpSession();
$session->open();
$session['i']=1;


$id=Yii::app()->request->getParam('id', 0);
$article=Article::model()->find('aid='.intval($id));
$session['cate']=$cate;
$session['iid']=$iid;
$session['i']=1;

if(empty($article)){
    $article=new Article;
    $article->cid=$iid;
}
$do=Yii::app()->request->getParam('do', '');
$url=$this->createUrl('manage/index',array('cate'=>$cate,'iid'=>$iid));
if($do=='delete'){
    if(isset($article->uid) && $article->uid==$uid){
        $article->enabled=0;
        if($article->save()){
            $this->oz_info('文章删除成功',$url);
            return;
        }
    } else {
        $this->oz_error('只有发布者可以删除该文章',$url);
        return;
    }
}elseif($do=='new'){

}

$criteria=new CDbCriteria;
$criteria->condition='`type`=3 AND enabled=1';
$criteria->order='sort ASC , createtime DESC';
$dataProvider=new CActiveDataProvider('Article',array(
    'criteria'=>$criteria,
    'pagination'=>array(
        'pageSize'=>20,
    ),
));

if(isset($_POST['Article'])){
    $article->attributes=$_POST['Article'];
    $article->title=isset($_POST['Article']['title']) ? $_POST['Article']['title'] : '';
    $article->content=isset($_POST['Article']['content']) ? $_POST['Article']['content'] : '';
    $article->uid=$uid;
    $article->audit=1;
    $article->excerpt='站内文章';
    $article->grade=0;
    $article->createtime=empty($article->createtime) ? date("Y-m-d H:i:s") : $article->createtime;
    $article->updatetime=date("Y-m-d H:i:s");
    $article->sort=0;
    $article->type=3;
    if($article->isNewRecord){
//        $article->excerpt='&nbsp;';
        $msg='添加成功';
    }else{
        $msg='保存成功';
    }

    if($article->validate()){
        if($article->save()){
            $this->oz_info($msg, $url);
            return;
        }
    }
}

?>
<style type="text/css">
    div.row label{
        width: 70px;
        display: inline-block;
    }
    div.row span.msg{
        margin-left: 10px;
        color: #c2c2c2;
    }
    div.row input{
        height: 30px;
        font-size: 14px;
        margin: 5px;
    }
    div.row textarea{
        font-size: 14px;
        margin: 5px;
    }
    div.hr{
        border-bottom: 1px solid #b3b3b3;
        margin-bottom: 20px;
    }
</style>
<div class="wp detailContent22" style="min-height: 1335px">
    <?php if($article->isNewRecord===true && $do!='new'): ?>
    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="table3 paddingbotton80 ">
        <tbody>
        <tr>
            <th bgcolor="#efefef" align="center" class="th1">标题</th>
            <th width="20%" bgcolor="#efefef" align="center" class="th1">发布时间</th>
            <th width="18%" bgcolor="#efefef" align="center">操作指令</th>
        </tr>
            <?php
            if(!empty($dataProvider->data))
                $this->widget('zii.widgets.CListView', array(
                    'dataProvider'=>$dataProvider,
                    'itemView'=>'xhzc/local',
                    'ajaxUpdate'=>false,
                    'template'=>"{items}\n{pager}" ,
                    //        'itemsTagName'=>'ul',
                    //        'itemsCssClass'=>'tb',
                ));
            ?>
        </tbody>
    </table>
    <script language="JavaScript">
        function new_a(){
            window.location.href="<?php echo $this->createUrl('manage/index',array('cate'=>$cate,'iid'=>$iid,'do'=>'new'));?>";
        }
    </script>

    <?php
    echo CHtml::button('新增',array('onclick'=>'javascript:new_a();')) ;
elseif(empty($article->isNewRecord) || $do=='new'):
    $this->new_editor('Article_content');
    ?>

    <?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'article-form',
    'enableAjaxValidation'=>false,
)); ?>

    <?php echo $form->errorSummary($article); ?>

    <div class="row">
        <?php echo $form->labelEx($article,'title'); ?>
        <?php
        if($article->isNewRecord){
            echo $form->textField($article,'title', array('size'=>'60'));
            echo $form->error($article,'title');
        }else{
            echo $form->textField($article,'title', array('size'=>'60','readonly'=>'readonly'));
            echo $form->error($article,'title');
            echo '不能修改标题';
        }
        ?>
    </div>

<!--    <div class="row">-->
<!--        --><?php //echo $form->labelEx($article,'出处'); ?>
<!--        --><?php //echo $form->textField($article,'excerpt', array('size'=>'40')); ?>
<!--        --><?php //echo $form->error($article,'excerpt'); ?>
<!--    </div>-->

    <div class="row">
        <?php echo $form->labelEx($article, 'content'); ?>
        <?php echo $form->textArea($article,'content'); ?>
        <?php echo $form->error($article,'content'); ?>
    </div>


    <div class="row buttons">
        <?php echo CHtml::submitButton($article->isNewRecord ? '添加文章' : '保存文章'); ?>
    </div>

    <?php $this->endWidget(); ?>

    <?php endif; ?>

    <div class="clear"></div>
</div>
